package com.loongxun.playwithyou.ui.activity.playuser;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.ui.fragment.MemberBillFragment;
import com.loongxun.playwithyou.ui.fragment.MemberOrderFragment;
import com.loongxun.playwithyou.ui.fragment.MemberSetupFragment;

import java.util.ArrayList;

/**
 * 陪玩人员个人中心
 * Created by Administrator on 2017/7/20.
 */
public class MemberOrderActivity extends FragmentActivity implements View.OnClickListener{

    //标题
    private TextView tv_top_title;
    private LinearLayout ll_member_order_left,ll_member_order_center,ll_member_order_right;

    private Context context;
    private ArrayList<Fragment> fragmentArrayList;
    private int currentIndex = 0;
    private Fragment mCurrentFrgment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_member_order);
        context = MemberOrderActivity.this;

        initView();
        initFragment();
    }

    //初始化Fragment
    private void initFragment() {
        fragmentArrayList = new ArrayList<>(3);
        fragmentArrayList.add(new MemberOrderFragment());
        fragmentArrayList.add(new MemberBillFragment());
        fragmentArrayList.add(new MemberSetupFragment());

        ll_member_order_left.setSelected(true);
        changeTab(0);
    }

    private void changeTab(int index) {
        currentIndex = index;
        ll_member_order_left.setSelected(index == 0);
        ll_member_order_center.setSelected(index == 1);
        ll_member_order_right.setSelected(index == 2);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //判断当前的Fragment是否为空，不为空则隐藏
        if (null != mCurrentFrgment) {
            ft.hide(mCurrentFrgment);
        }
        //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentArrayList.get(currentIndex).getClass().getName());

        if (null == fragment) {
            //如fragment为空，则之前未添加此Fragment。便从集合中取出
            fragment = fragmentArrayList.get(index);
        }
        mCurrentFrgment = fragment;
        //判断此Fragment是否已经添加到FragmentTransaction事物中
        if (!fragment.isAdded()) {
            ft.add(R.id.fragment, fragment, fragment.getClass().getName());
        } else {
            ft.show(fragment);
        }
        ft.commit();
    }


    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("陪练个人中心");

//        lv_member_order = (ListView) findViewById(R.id.lv_member_order);

        ll_member_order_left = (LinearLayout) findViewById(R.id.ll_member_order_left);
        ll_member_order_center = (LinearLayout) findViewById(R.id.ll_member_order_center);
        ll_member_order_right = (LinearLayout) findViewById(R.id.ll_member_order_right);

        ll_member_order_left.setOnClickListener(this);
        ll_member_order_center.setOnClickListener(this);
        ll_member_order_right.setOnClickListener(this);

        ll_member_order_left.setTag(0);
        ll_member_order_center.setTag(1);
        ll_member_order_right.setTag(2);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_member_order_left:
            case R.id.ll_member_order_center:
            case R.id.ll_member_order_right:
                changeTab((Integer) view.getTag());
                break;
            default:
                break;
        }
    }


}
